package com.hqyj.ssm_project.controller;

import com.hqyj.ssm_project.pojo.Idcard;
import com.hqyj.ssm_project.pojo.User;
import com.hqyj.ssm_project.service.IdcardService;
import com.hqyj.ssm_project.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class IdcardController {
    @Autowired
    private IdcardService idcardService;
    @Autowired
    private UserService userService;
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @RequestMapping(value = "/idcardPage.do")
    public String IdcardPage(Model model) {
        List<Idcard> allIdcard = idcardService.findAllIdcard();
        model.addAttribute("idcardlist", allIdcard);
        return "IdcardPage";
    }
    @RequestMapping(value = "/addIdcardPage.do")
    public String addIdcardPage(Model model) {
        List<User> noidcardUserlist = userService.findUserNoidcard();
        logger.debug(String.valueOf(noidcardUserlist));
        model.addAttribute("userlist", noidcardUserlist);
        return "addIdcardPage";
    }
    @RequestMapping(value = "/addIdcard.do")
    public String addIdcard(Idcard idcard) {
        logger.debug("addIdcard-----------------------");
        idcardService.insertidcard(idcard);
        Integer idcardId = idcard.getIdcardId();
        logger.debug(String.valueOf(idcardId));
        Integer userId = idcard.getUser().getUserId();
        User user = new User();
        user.setUserId(userId);
        user.setIdcardId(idcardId);
        logger.debug("addIdcard---user-------" + user);
        userService.updateByPrimaryKeySelective(user);
        logger.debug(String.valueOf(idcard));
        return "redirect:idcardPage.do";
    }
    @RequestMapping(value = "updateIdcardPage.do")
    public String updateIdcardPage(Integer id, Model model) {
        logger.debug("updateIdcardPage-----------------------");
        Idcard idcard = idcardService.selectIdcardAndUser(id);
        logger.debug(String.valueOf(idcard));
        model.addAttribute("idcard", idcard);
        return "updateIdcardPage";
    }
    @RequestMapping(value = "updateIdcard.do")
    public String updateIdcard(Idcard idcard){
        logger.debug("updateIdcard-----------------------");
        logger.debug(String.valueOf(idcard));
        idcardService.updateByPrimaryKey(idcard);
        return "redirect:idcardPage.do";
    }
    @RequestMapping(value = "/deleteIdcard.do" )
    public String deleteIdcard(Integer id){
        logger.debug("deleteIdcard-----------------------");
        Idcard idcard = idcardService.selectIdcardAndUser(id);
        logger.debug("idcard"+idcard);
        Integer userId = idcard.getUser().getUserId();
        userService.updateIdcardByUser(userId);
        idcardService.deleteByPrimaryKey(id);
        return "redirect:idcardPage.do";
    }
}
